from zope.interface import implements, Interface

from Products.Five import BrowserView
from Products.CMFCore.utils import getToolByName
from zope.component import getMultiAdapter
from plone.memoize.instance import memoize
from Acquisition import aq_inner

from my315ok.xtjxwcontents import xtjxwcontentsMessageFactory as _
from my315ok.xtjxwcontents.browser.barsview import barsView
from my315ok.xtjxwcontents.interfaces.jiaxiao import Ijiaxiao


class IrecommendschoolView(Interface):
    """
    recommendschool view interface
    """

    def test():
        """ test method"""


class recommendschoolView(barsView):
    """
    recommendschool browser view
    """
    implements(IrecommendschoolView)

    @memoize
    def fetchjiaxiao(self):
        context = aq_inner(self.context)
        catalog = getToolByName(context, 'portal_catalog')
        sepath= '/'.join(context.getPhysicalPath())
        limit = 7 
        query = {'object_provides':Ijiaxiao.__identifier__,
                 'sort_on':'renqizhishu',
                 'sort_order':'reverse',
                 'review_state':'published',
                 'path':sepath, 
                 'sort_limit':limit,        
                   }        
        sd = catalog(query)[:limit]
        return sd
    @memoize
    def imgitems_fast(self,scale="large",tab=u"，"):
        brains = self.fetchjiaxiao()
        items = {}
        items['titl'] = []
        items['url'] = []
        items['src'] = []
        items['txt'] = []
        if scale == "orig":
            for bn in brains:
                base = bn.getURL()                
                items['titl'].append(bn.Title)
                dsp = self.splittxt(bn.Description, tab)
                items['txt'].append(dsp)
#                if type(bn.link2) =="Missing":
#                    url = ""
#                else:
#                    url = bn.link2
                items['url'].append(base)
                imgsrc = base + '/' + 'main.png/image'
#                jiaxiaoview = getMultiAdapter((obj,self.request),name=u"jiaxiao_view")
#                del obj
#                imgbn = jiaxiaoview.fetchjiaxiaoimage()
                items['src'].append(imgsrc)                 
#                items['src'].append(base + "/image")          
            return items
        else:            
            for bn in brains:
                base = bn.getURL()
                items['titl'].append(bn.Title)
                dsp = self.splittxt(bn.Description, tab)
                items['txt'].append(dsp)
                items['url'].append(base)
                imgsrc = base + '/' + 'main.png/image_' + scale
                items['src'].append(imgsrc)         
            return items